package buaa.leetcode.Tree;

import java.util.LinkedList;
import java.util.Queue;

import buaa.leetcode.utilBeans.TreeLinkNode;

public class PopulatingNextRightPointersInEachNode {
	 public static void connect(TreeLinkNode root) {
	        if(root == null){
	            return;
	        }
	        Queue<TreeLinkNode> queue = new LinkedList<>();
	        queue.add(root);
	        TreeLinkNode currNode = root;
	        while(!queue.isEmpty()){
	            currNode = queue.remove();
	            if(currNode.left!=null){
	                queue.add(currNode.left);
	                queue.add(currNode.right);
	            }
	            currNode.left.next = currNode.right;
	            if(currNode.next != null && currNode.next.left != null){
	                currNode.right.next = currNode.next.left;
	            }else{
	                currNode.right.next = null;
	            }
	        }
	        
	    }
}
